<?php

/**
 * user actions.
 *
 * @package    buzzwork
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 1139 2006-04-07 08:46:23Z fabien $
 */
class userActions extends sfActions
{
  public function executeEdit ()
  {
    $this->user = UserPeer::retrieveByPk($this->getUser()->getSubscriberId());
    $this->forward404Unless($this->user);
  }

  public function executeUpdate()
  {
    $user = UserPeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($user);

    $user->setId($this->getRequestParameter('id'));
    $user->setFirstname($this->getRequestParameter('firstname'));
    $user->setLastname($this->getRequestParameter('lastname'));
    $user->setTitle($this->getRequestParameter('title'));
    $user->setPhone($this->getRequestParameter('phone'));
    $user->setMobile($this->getRequestParameter('mobile'));
    $user->setFax($this->getRequestParameter('fax'));

    $user->save();
    return $this->redirect('@current_user_profile');
  }

  public function executePasswordUpgrade()
  {
    if ($this->getRequest()->getMethod() != sfRequest::POST)
    {
      return sfView::SUCCESS; 
    }
    else
    {
      $oldpassword = $this->getRequestParameter('oldpassword');
      $user = UserPeer::retrieveByPk($this->getUser()->getSubscriberId());
      // password is OK?
      if (sha1($user->getSalt().$oldpassword) == $user->getSha1Password())
      {
        // old password is correct check if the new password is correct
        if ($this->getRequestParameter('password1') == $this->getRequestParameter('password2'))
        {
          //then update and redirect
          $user->setPassword($this->getRequestParameter('password1'));
          $user->save();
        }
        else
        {
          $this->getRequest()->setError('password2', 'La nuova password non è stata confermata correttamente');
          return sfView::SUCCESS;
        }
        $this->getRequest()->setError('success', 'La password è stata cambiata!');
        return $this->redirect('@current_user_profile');
      }
      else
      {
        $this->getRequest()->setError('oldpassword', 'La vecchia password non è corretta');
        return sfView::SUCCESS;
      }
    }
  }

  public function executeShow()
  {
    if ($this->hasRequestParameter('username'))
    {
      $this->subscriber = UserPeer::getUserFromUsername($this->getRequestParameter('username'));
    }
    else
    {
      // show my information
      $this->subscriber = $this->getUser()->getSubscriber();
    }
    $this->forward404Unless($this->subscriber);
  }


  public function executeLogin()
  {
    if ($this->getRequest()->getMethod() != sfRequest::POST)
    {
      // display the form
      $this->getRequest()->getParameterHolder()->set('referer', $this->getRequest()->getReferer());
   
      return sfView::SUCCESS;
    }
    else
    {
      // handle the form submission
      // redirect to last page
      return $this->redirect($this->getRequestParameter('referer', '@homepage'));
    }
  }

  public function executeLogout()
  {
    $this->getUser()->signOut();
   
    $this->redirect('@homepage');
  }


  public function handleErrorLogin()
  {
    return sfView::SUCCESS;
  }
}
?>